package com.tongji.entity.linkman;

import com.ibeetl.admin.core.util.DateUtil;
import com.tongji.entity.宣传员;
import com.tongji.run.manager.ManagerFactory;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;

import java.util.*;

public class 宣传员费用 {
    private 宣传员 linkman;
    private 宣传员费用 合并宣传员费用;
    private List<朗程费用明细> 附加误工费List = new ArrayList<>();
    private List<朗程费用明细> 朗程宣传费用List = new ArrayList<>();
    private List<朗程费用明细> 手开宣传费用List = new ArrayList<>();
    private 宣传员费用明细 费用明细 = new 宣传员费用明细();
    private 宣传员费用明细 合并后费用明细 = new 宣传员费用明细();

    private Date dateRangeStart;
    private Date dateRangeEnd;

    public String dateRange;
    public static String root = "E:\\文档\\第三方费用统计\\路费\\";


    public void 统计() {

        List<朗程费用明细> all附加误工费List = new ArrayList();
        List<朗程费用明细> all朗程宣传费用List = new ArrayList();
        List<朗程费用明细> all手开宣传费用List = new ArrayList();


        统计朗程附加误工费和管理费(this.linkman.getName());
        统计朗程及线下宣传费用(this.linkman.getName());

        Map data = new HashMap();
        data.put("today", new Date());
        data.put("expense", this);

        if (this.linkman.getName().equals("曹建涛")) {
            this.linkman.setName("曹桂勇");
        }
        if (this.linkman.getName().equals("张秀春")) {
            this.linkman.setName("岳良全");
        }
        //处理被合并的宣传员
        if (this.linkman.getHebingName() != null) {
            宣传员 合并宣传员 = new 宣传员();
            合并宣传员.setName(this.linkman.getHebingName());
            合并宣传员.setXuhao(this.linkman.getXuhao());
            宣传员费用 合并宣传员费用 = new 宣传员费用();
            合并宣传员费用.setDateRangeStart(dateRangeStart);
            合并宣传员费用.setDateRangeEnd(dateRangeEnd);
            合并宣传员费用.setLinkman(合并宣传员);
            合并宣传员费用.统计();

            this.set合并宣传员费用(合并宣传员费用);

            all附加误工费List.addAll(this.合并宣传员费用.get附加误工费List());
            all朗程宣传费用List.addAll(this.合并宣传员费用.get朗程宣传费用List());
            all手开宣传费用List.addAll(this.合并宣传员费用.get手开宣传费用List());
        }
        all附加误工费List.addAll(this.get附加误工费List());
        all朗程宣传费用List.addAll(this.get朗程宣传费用List());
        all手开宣传费用List.addAll(this.get手开宣传费用List());

        计算费用(this.合并后费用明细,all附加误工费List, all朗程宣传费用List, all手开宣传费用List);

        if (this.get合并宣传员费用() != null) {
            ManagerFactory.createExcel(宣传员费用.root + "\\" + this.getDateRange() + "\\费用统计",
                    "excelTemplates/linkman/宣传员费用统计-合并.xlsx",
                    "\\" + this.getLinkman().getXuhao() + "-0-" + this.getLinkman().getName() + (this.getLinkman().getHebingName() == null ? "" : ("-" + this.getLinkman().getHebingName()))  + "-统计表-" + dateRange + ".xlsx",data);
        } else {
            ManagerFactory.createExcel(宣传员费用.root + "\\" + this.getDateRange() + "\\费用统计",
                    "excelTemplates/linkman/宣传员费用统计.xlsx",
                    "\\" + this.getLinkman().getXuhao() + "-" + this.getLinkman().getName() + "-统计表-" + dateRange + ".xlsx",data);
        }
        if (this.get朗程宣传费用List().size() > 0) {
            System.out.println(data);
            ManagerFactory.createExcel(宣传员费用.root + "\\" + this.getDateRange() + "\\费用统计",
                    "excelTemplates/linkman/宣传员朗程宣传费用明细.xlsx",
                    "\\" + this.getLinkman().getXuhao() + "-" + this.getLinkman().getName() + "-宣传费用明细-" + dateRange + ".xlsx", data);
        }
    }


    //路费按照采浆时间统计，在朗程系统中，必须先将统计时间范围内的交通补贴全部通过审核
    public List<朗程费用明细> 统计朗程附加误工费和管理费(String linkmanName) {
        if (dateRangeStart != null && dateRangeEnd != null) {

            SQLManager taibangSqlManager = ManagerFactory.taibangSqlManager();

            String sql = "select \n" +
                "payment.linkmanId as 宣传员ID,\n" +
                "linkman.linkmanNo as 宣传员编号,\n" +
                "linkman.linkmanName as 宣传员姓名,\n" +
                "(select collection.processNo from t_bims_plasma_collection as collection where collection.donorId = donor.donorId and collection.beginDate between CONCAT(LEFT(payment.collectDate,8),'0000000') and CONCAT(LEFT(payment.collectDate,8),'2300000') order by collection.beginDate desc limit 0,1) as 登记号,\n" +
                "donor.donorNo as 浆员编号,\n" +
                "donor.donorName as 浆员姓名,\n" +
                "area.cname as 区域名称,\n" +
                "donor.idAddress as 采浆地址,\n" +
                "DATE_FORMAT(payment.collectDate,'%Y-%m-%d') as 采浆日期,\n" +
                "donor.phone as '电话',\n" +
                "'交通补贴' as 费用类型,\n" +
                "payment.amount as 费用金额,\n" +
                "donor.remark as 浆员备注\n" +
                " from t_bims_expense_payment as payment \n" +
                "left join t_bims_donor as donor on donor.donorId = payment.donorId \n" +
                "left join t_bims_linkman as linkman on linkman.linkmanId = payment.linkmanId\n" +
                "left join t_bims_code_area as area on area.ccode = payment.codeArea\n" +
                "where \n" +
                "payment.collectDate between ? and ?  and payment.checkResult = 'Y' and payment.isPay = 'N' and payment.codePremium = 'TRANSPORTEXPENSE' ##or payment.codePremium = 'ORGANIZATIONEXPENSE')\n" +
                "and linkman.linkmanName = ?\n" +
                "order by payment.checkDate";

            this.附加误工费List = taibangSqlManager.execute(new SQLReady(sql,
                new Object[] {DateUtil.format(this.dateRangeStart,"yyyyMMddHHmmsss"),DateUtil.format(this.dateRangeEnd,"yyyyMMddHHmmsss"),"" + linkmanName + "" }), 朗程费用明细.class);

            计算费用(this.费用明细,this.附加误工费List, null, null);
        } else {
            System.out.println("起始时间或宣传员信息有误");
        }
        return null;
    }


    //检疫期等宣传费用按照审核日期统计，宣传费用包括：检疫期30，超半年50，介绍费120或160，补充介绍费40
    public List<朗程费用明细> 统计朗程及线下宣传费用(String linkmanName) {
        if (dateRangeStart != null && dateRangeEnd != null && linkman != null) {

            SQLManager taibangSqlManager = ManagerFactory.taibangSqlManager();

            String sql = "select \n" +
                    "payment.linkmanId as 宣传员ID,\n" +
                    "linkman.linkmanNo as 宣传员编号,\n" +
                    "linkman.linkmanName as 宣传员姓名,\n" +
                    "(select collection.processNo from t_bims_plasma_collection as collection where collection.donorId = donor.donorId and collection.beginDate between CONCAT(LEFT(payment.collectDate,8),'0000000') and CONCAT(LEFT(payment.collectDate,8),'2300000') order by collection.beginDate desc limit 0,1) as 登记号,\n" +
                    "donor.donorNo as 浆员编号,\n" +
                    "donor.donorName as 浆员姓名,\n" +
                    "area.cname as 区域名称,\n" +
                    "donor.idAddress as 采浆地址,\n" +
                    "DATE_FORMAT(payment.collectDate,'%Y-%m-%d') as 采浆日期,\n" +
                    "donor.phone as '电话',\n" +
                    "'宣传费用' as 费用类型,\n" +
                    "payment.amount as 费用金额,\n" +
                    "donor.remark as 浆员备注\n" +
                    " from t_bims_expense_payment as payment \n" +
                    "left join t_bims_donor as donor on donor.donorId = payment.donorId \n" +
                    "left join t_bims_linkman as linkman on linkman.linkmanId = payment.linkmanId\n" +
                    "left join t_bims_code_area as area on area.ccode = payment.codeArea\n" +
                    "where \n" +
                    "payment.checkDate between ? and ? and payment.checkResult = 'Y' and payment.isPay = 'N' and payment.codePremium = 'ORGANIZATIONEXPENSE'\n" +
                    "and linkman.linkmanName = ?\n" +
                    "order by payment.checkDate";

            this.朗程宣传费用List = taibangSqlManager.execute(new SQLReady(sql,
                    new Object[] {DateUtil.format(this.dateRangeStart,"yyyyMMddHHmmsss"),DateUtil.format(this.dateRangeEnd,"yyyyMMddHHmmsss"),"" + linkmanName + "" }), 朗程费用明细.class);
            计算费用(this.费用明细,null, this.朗程宣传费用List, null);

            SQLManager jiangzhanSqlManager = ManagerFactory.jiangzhanSqlManager();
            this.手开宣传费用List = jiangzhanSqlManager.execute(new SQLReady("select 领取人 as 宣传员姓名,金额 as 费用金额,类型 as 费用类型 from 宣传员费用登记表 where 日期 between ? and ? and 领取人 = ? ",
                    new Object[] {DateUtil.format(this.dateRangeStart,"yyyyMMddHHmmsss"),DateUtil.format(this.dateRangeEnd,"yyyyMMddHHmmsss"),"" + linkmanName + "" }), 朗程费用明细.class);
            计算费用(this.费用明细,null, null, this.手开宣传费用List);
        } else {
            System.out.println("起始时间或宣传员信息有误");
        }

        return null;
    }

    public void 计算费用(宣传员费用明细 宣传员费用明细, List 附加误工费List, List 朗程宣传费用List, List 手开宣传费用List) {
        if (附加误工费List != null) {
            for (Iterator<朗程费用明细> iterator = 附加误工费List.iterator(); iterator.hasNext(); ) {
                朗程费用明细 next =  iterator.next();
                Double 附加误工费 = next.get费用金额();
                Double 管理费 = 交通补助表.车主路费Map.get(next.get区域名称()) - 附加误工费;
                next.set("管理费",管理费);

                宣传员费用明细.add附加误工费(附加误工费);
                宣传员费用明细.add管理费(管理费);
                宣传员费用明细.add附加误工费数量(1);
                宣传员费用明细.add管理费数量(1);
            }
        }
        if (朗程宣传费用List != null) {
            for (Iterator<朗程费用明细> iterator = 朗程宣传费用List.iterator(); iterator.hasNext(); ) {
                朗程费用明细 next =  iterator.next();
                宣传员费用明细.add朗程宣传费合计(next.get费用金额());
                宣传员费用明细.add朗程宣传费数量(1);

                if (next.get费用金额() == 30D) {
                    宣传员费用明细.add朗程检疫期费用(next.get费用金额());
                    宣传员费用明细.add朗程检疫期费用数量(1);
                }
                if (next.get费用金额() == 40D) {
                    宣传员费用明细.add补充介绍费(next.get费用金额());
                    宣传员费用明细.add补充介绍费数量(1);
                }
                if (next.get费用金额() == 50D) {
                    宣传员费用明细.add朗程超半年费用(next.get费用金额());
                    宣传员费用明细.add朗程超半年费用数量(1);
                }

                if (next.get费用金额() == 60D) {
                    宣传员费用明细.add朗程引领费(next.get费用金额());
                    宣传员费用明细.add朗程引领费数量(1);
                }
                if (next.get费用金额() == 120D || next.get费用金额() == 160D || next.get费用金额() == 90D || next.get费用金额() == 80D || next.get费用金额() == 100D) {
                    宣传员费用明细.add朗程介绍费(next.get费用金额());
                    宣传员费用明细.add朗程介绍费数量(1);
                }
            }
        }
        if (手开宣传费用List != null) {
            for (Iterator<朗程费用明细> iterator = 手开宣传费用List.iterator(); iterator.hasNext(); ) {
                朗程费用明细 next =  iterator.next();
                if ("检疫期".equals(next.get费用类型())) {
                    宣传员费用明细.add手开检疫期费用(next.get费用金额());
                    宣传员费用明细.add手开检疫期费用数量(1);
                }
                if ("超半年".equals(next.get费用类型())) {
                    宣传员费用明细.add手开超半年费用(next.get费用金额());
                    宣传员费用明细.add手开超半年费用数量(1);
                }
                if ("介绍费".equals(next.get费用类型())) {
                    宣传员费用明细.add手开介绍费(next.get费用金额());
                    宣传员费用明细.add手开介绍费数量(1);
                }
                if ("补充介绍费".equals(next.get费用类型())) {
                    宣传员费用明细.add补充介绍费(next.get费用金额());
                    宣传员费用明细.add补充介绍费数量(1);
                }
                if ("补充营养费".equals(next.get费用类型())) {
                    宣传员费用明细.add补充营养费(next.get费用金额());
                    宣传员费用明细.add补充营养费数量(1);
                }
                if ("引领费".equals(next.get费用类型())) {
                    宣传员费用明细.add朗程引领费(next.get费用金额());
                    宣传员费用明细.add朗程引领费数量(1);
                }
                if ("不合格附加误工费".equals(next.get费用类型())) {
                    宣传员费用明细.add不合格附加误工费(next.get费用金额());
                    宣传员费用明细.add不合格附加误工费数量(1);
                }
                if ("其他费用".equals(next.get费用类型())) {
                    宣传员费用明细.add其他费用(next.get费用金额());
                    宣传员费用明细.add其他费用数量(1);
                }
            }
        }
    }

    public List<朗程费用明细> get附加误工费List() {
        return 附加误工费List;
    }

    public void set附加误工费List(List<朗程费用明细> 附加误工费List) {
        this.附加误工费List = 附加误工费List;
    }



    public 宣传员 getLinkman() {
        return linkman;
    }

    public void setLinkman(宣传员 linkman) {
        this.linkman = linkman;
    }

    public 宣传员费用明细 get费用明细() {
        return 费用明细;
    }

    public void set费用明细(宣传员费用明细 费用明细) {
        this.费用明细 = 费用明细;
    }

    public Date getDateRangeStart() {
        return dateRangeStart;
    }

    public void setDateRangeStart(Date dateRangeStart) {
        this.dateRangeStart = dateRangeStart;
    }

    public Date getDateRangeEnd() {
        return dateRangeEnd;
    }

    public void setDateRangeEnd(Date dateRangeEnd) {
        this.dateRangeEnd = dateRangeEnd;
    }

    public List<朗程费用明细> get朗程宣传费用List() {
        return 朗程宣传费用List;
    }

    public void set朗程宣传费用List(List<朗程费用明细> 朗程宣传费用List) {
        this.朗程宣传费用List = 朗程宣传费用List;
    }

    public String getDateRange() {
        this.dateRange = DateUtil.format(dateRangeStart, "yyyy年MM月dd日") + "至" + DateUtil.format(dateRangeEnd, "yyyy年MM月dd日");
        return this.dateRange;
    }

    public void setDateRange(String dateRange) {
        this.dateRange = dateRange;
    }

    public List<朗程费用明细> get手开宣传费用List() {
        return 手开宣传费用List;
    }

    public void set手开宣传费用List(List<朗程费用明细> 手开宣传费用List) {
        this.手开宣传费用List = 手开宣传费用List;
    }

    public 宣传员费用 get合并宣传员费用() {
        return 合并宣传员费用;
    }

    public void set合并宣传员费用(宣传员费用 合并宣传员费用) {
        this.合并宣传员费用 = 合并宣传员费用;
    }

    public 宣传员费用明细 get合并后费用明细() {
        return 合并后费用明细;
    }

    public void set合并后费用明细(宣传员费用明细 合并后费用明细) {
        this.合并后费用明细 = 合并后费用明细;
    }

    @Override
    public String toString() {
        return "宣传员费用{" +
                "linkman=" + linkman.getName() +
                ", 费用明细=" + 费用明细 +
                ", dateRangeStart=" + dateRangeStart +
                ", dateRangeEnd=" + dateRangeEnd +
                '}';
    }
}
